/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.disablers

import net.ccbluex.liquidbounce.config.types.nesting.ToggleableConfigurable
import net.ccbluex.liquidbounce.event.events.PlayerTickEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.ModuleDisabler
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.ModuleScaffold.isTowering
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.techniques.normal.ScaffoldTellyFeature.isTellyBridging
import net.ccbluex.liquidbounce.utils.client.sendStopSneaking
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
import kotlin.random.Random

/**
 * Tested on eu.loyisa.cn, anticheat-test.com
 * Author: TrimoneWasTaken
 *
 * Sending start sprint and stop sprint disables vulcan direction sprint checks,
 * since vulcan thinks you are just spamming sprint.
 *
 * Most vulcan scaffold checks (including limit) check if you have sneaked in
 * the last 15 ticks. By using packets to unsneak, you can exploit this check and
 * disable limit checks.
 */

internal object DisablerVulcanScaffold : ToggleableConfigurable(ModuleDisabler, "VulcanScaffold", false) {

    @Suppress("ComplexCondition", "unused")
    private val tickHandler = handler<PlayerTickEvent> {
        if (player.isInFluid || player.isTouchingWater || player.isDead || player.isHoldingOntoLadder
            || player.abilities.flying) {
            // Not in a state where scaffold is possible or useful
            return@handler
        }

        // Fix for the disabler not triggering while telly bridging
        if (isTellyBridging && player.age % 9 == 0 && Random.nextFloat() <= 0.7f) {
            sendStopSneaking()
        }

        if (player.isOnGround && isTowering && Random.nextFloat() <= 0.2f) {
            /*
             * We use probability to send unsneak when the player is on ground and is towering to fix the
             * flag of scaffold-packets check and scaffold K check, this implementation
             * works with low hop towers like karhu, or any tower that sometimes hit the ground.
             */

            sendStopSneaking()
        }

        network.sendPacket(ClientCommandC2SPacket(player,ClientCommandC2SPacket.Mode.START_SPRINTING))
        network.sendPacket(ClientCommandC2SPacket(player,ClientCommandC2SPacket.Mode.STOP_SPRINTING))

        if (player.age % 9 == 0 && player.isOnGround && !isTellyBridging && !isTowering) {
            sendStopSneaking()
        }
    }
}
